<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\PostCatlog
 *
 * @property int $catid
 * @property int $fid 父级分类
 * @property string|null $name 分类名称
 * @property string|null $identifier
 * @property string $icon 图片
 * @property int $level 级别
 * @property int $enable 是否可选
 * @property int $available 是否可用
 * @property int $displayorder 显示顺序
 * @property string|null $keywords 关键字
 * @property string|null $description 描述
 * @property string|null $template_index 首页模板
 * @property string|null $template_list 列表页模板
 * @property string|null $template_detail 详细页模板
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PostCatlog[] $childs
 * @property-read \App\Models\PostCatlog $parent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PostItem[] $posts
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog enable()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereAvailable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereCatid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereDisplayorder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereEnable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereFid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereIcon($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereIdentifier($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereKeywords($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereTemplateDetail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereTemplateIndex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostCatlog whereTemplateList($value)
 * @mixin \Eloquent
 */
class PostCatlog extends Model
{
    protected $table = 'post_catlog';
    protected $primaryKey = 'catid';
    protected $fillable = [
        'catid','fid','name','identifier','icon','level','enable','available',
        'displayorder','keywords','description','template_index','template_list','template_detail'
    ];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::addGlobalScope('available', function (Builder $builder) {
            return $builder->where('available', 1);
        });
    }

    /**
     * @param $value
     * @return string
     */
    public function getIconAttribute($value)
    {
        return image_url($value);
    }

    /**
     * @param $value
     */
    public function setIconAttribute($value)
    {
        $this->attributes['icon'] = str_replace(material_url(), '', $value);
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeEnable(Builder $query)
    {
        return $query->where('enable', 1);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function posts()
    {
        return $this->hasMany(PostItem::class, 'catid', 'catid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function childs()
    {
        return $this->hasMany(PostCatlog::class, 'fid', 'catid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(PostCatlog::class, 'fid', 'catid');
    }
}
